package njust.cms.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import njust.tables.article.InfoArticle;
import njust.tables.article.InfoArticleDAO;
import njust.tables.articleawards.InfoArticleAwards;
import njust.tables.articleawards.InfoArticleAwardsDAO;
import njust.tables.book.InfoBook;
import njust.tables.book.InfoBookDAO;
import njust.tables.bookwrite.InfoBookWriter;
import njust.tables.bookwrite.InfoBookWriterDAO;
import njust.tables.checkup.InfoCheckup;
import njust.tables.checkup.InfoCheckupDAO;
import njust.tables.foreign.InfoForeign;
import njust.tables.foreign.InfoForeignDAO;
import njust.tables.groupbuild.InfoGroupBuild;
import njust.tables.groupbuild.InfoGroupBuildDAO;
import njust.tables.ipr.InfoIpr;
import njust.tables.ipr.InfoIprDAO;
import njust.tables.meeting.InfoMeeting;
import njust.tables.meeting.InfoMeetingDAO;
import njust.tables.repoj.InfoRePoj;
import njust.tables.repoj.InfoRePojDAO;
import njust.tables.support.InfoSupport;
import njust.tables.support.InfoSupportDAO;
import njust.tables.train.InfoTrain;
import njust.tables.train.InfoTrainDAO;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
 * 
 * @author gzq
 * 信息处理控制器
 * 1.科研训练 repoj
 * 2.研究生培养(每年) train
 * 3.研究生获优秀学位论文情况  article
 * 4.研究生获国家、江苏省科研创新计划资助情况  support
 * 5.团队建设   groupbuild
 * 6.出版教材   book
 * 7.知识产权  ipr
 * 8.成果验收鉴定  checkup
 * 9.主办学术会议情况  meeting
 * 10.教师境外进修(合作研究)情况  foreign
 */
@Controller
@RequestMapping("/info")
public class Info {
	
	@Resource(name="InfoRePojDAO")
	private InfoRePojDAO infoRePojdao;            //1.科研项目
	@Resource(name="InfoTrainDAO")
	private InfoTrainDAO infoTraindao;            //2.研究生培养(每年)
	@Resource(name="InfoArticleDAO")
	private InfoArticleDAO infoArticledao;        //3.研究生获优秀学位论文情况
	@Resource(name="InfoArticleAwardsDAO")
	private InfoArticleAwardsDAO infoArticleAwardsdao;
	@Resource(name="InfoSupportDAO")
	private InfoSupportDAO infoSupportdao;        //4.研究生获优秀学位论文情况
	@Resource(name="InfoGroupBuildDAO")
	private InfoGroupBuildDAO infoGroupBuilddao;  //5.团队建设
	@Resource(name="InfoBookDAO")
	private InfoBookDAO infoBookdao;              //6.出版教材
	@Resource(name="InfoBookWriterDAO")
	private InfoBookWriterDAO infoBookWriterdao;
	@Resource(name="InfoIprDAO")
	private InfoIprDAO infoIprdao;                //7.知识产权
	@Resource(name="InfoCheckupDAO")
	private InfoCheckupDAO infoCheckupdao;        //8.成果验收坚定
	@Resource(name="InfoMeetingDAO")
	private InfoMeetingDAO infoMeetingdao;        //9.主办学术会议情况
	@Resource(name="InfoForeignDAO")
	private InfoForeignDAO infoForeigndao;        //10.教师境外进修(合作研究)情况

	/**
	 * 信息页面主页
	 * @return
	 */
	@RequestMapping("home")
	public String infoHome()
	{
		return "info_home";
	}
	
	/**
	 * 前往信息填写页面
	 * @param page 页面标识
	 * @return
	 */
	@RequestMapping("{page}")
	public String writeInfo(@PathVariable("page") String page)
	{
		return "info_write_"+page;
	}
	
	/**
	 * 判断文章编号是否存在
	 * @param aid
	 * @return
	 */
	@RequestMapping("exist_article_{aid}")
	public @ResponseBody Map<String, Object> existArticle(@PathVariable("aid") Integer aid) throws IOException
	{
		Map<String, Object> map=new HashMap<>();
		List<Object> l=infoArticledao.findByAId(aid);
		if(l.size()!=0)
			map.put("result", "true");
		else
			map.put("result", "false");
		return map;
	}
	
	/**
	 * 展示信息列表
	 * @param page
	 * @return
	 */
	@RequestMapping("list_{page}")
	public String showInfoList(HttpServletRequest request, @PathVariable("page") String page)
	{
		switch (page) {
		case "repojs":
			request.setAttribute("repojs", infoRePojdao.findAll());
			break;
		case "trains":
			request.setAttribute("trains", infoTraindao.findAll());
			break;
		case "articles":
			request.setAttribute("articles", infoArticledao.findAll());
			break;
		case "supports":
			request.setAttribute("supports", infoSupportdao.findAll());
			break;
		case "groupbuilds":
			request.setAttribute("groupbuilds", infoGroupBuilddao.findAll());
			break;
		case "books":
			request.setAttribute("books", infoBookdao.findAll());
			break;
		case "iprs":
			request.setAttribute("iprs", infoIprdao.findAll());
			break;
		case "checkups":
			request.setAttribute("checkups", infoCheckupdao.findAll());
			break;
		case "meetings":
			request.setAttribute("meetings", infoMeetingdao.findAll());
			break;
		case "foreigns":
			request.setAttribute("foreigns", infoForeigndao.findAll());
			break;
		default:
			break;
		}
		
		return "info_list";
	}
	
	/**
	 * 展示信息页面
	 * @param page 页面标识
	 * @param id 
	 * @return
	 */
	@RequestMapping("show/{page}_{id}")
	public String showInfoItem(HttpServletRequest request, @PathVariable("page") String page, @PathVariable("id") Integer id){
		switch (page) {
		case "repoj":
			request.setAttribute("repoj", infoRePojdao.findById(id));
			break;
		case "train":
			request.setAttribute("train", infoTraindao.findById(id));
			break;
		case "article":
			request.setAttribute("article", infoArticledao.findById(id));
			break;
		case "support":
			request.setAttribute("support", infoSupportdao.findById(id));
			break;
		case "groupbuild":
			request.setAttribute("groupbuild", infoGroupBuilddao.findById(id));
			break;
		case "book":
			request.setAttribute("book", infoBookdao.findById(id));
			break;
		case "ipr":
			request.setAttribute("ipr", infoIprdao.findById(id));
			break;
		case "checkup":
			request.setAttribute("checkup", infoCheckupdao.findById(id));
			break;
		case "meeting":
			request.setAttribute("meeting", infoMeetingdao.findById(id));
			break;
		case "foreign":
			request.setAttribute("foreign", infoForeigndao.findById(id));
			break;
		default:
			break;
		}
		
		return "info_show_"+page;
	}
	
	/**
	 * 信息提交方法
	 */
	
	//1.科研项目
	@RequestMapping("submit_repoj")
	public String submitRepoj(HttpServletRequest request,InfoRePoj infoRePoj)
	{
		HttpSession session=request.getSession();
		String publisher=(String) session.getAttribute("Name");
		infoRePoj.setPublisher(publisher);
		infoRePoj.setPubDate(new Date());
		infoRePojdao.save(infoRePoj);    //保存信息
		
		return "redirect:show/repoj_"+infoRePoj.getRId();
	}
	
	//2.研究生培养
	@RequestMapping("submit_train")
	public String submitTrain(HttpServletRequest request,InfoTrain infoTrain)
	{
		HttpSession session=request.getSession();
		String publisher=(String) session.getAttribute("Name");
		infoTrain.setPublisher(publisher);
		infoTrain.setPubDate(new Date());
		infoTraindao.save(infoTrain);    //保存信息
		
		return "redirect:show/train_"+infoTrain.getId();
	}
	
	//3.研究生获优秀学位论文情况 
	@RequestMapping("submit_article")
	public String submitArticle(HttpServletRequest request,InfoArticle infoArticle){
		HttpSession session =request.getSession();
		String publisher=(String) session.getAttribute("Name");
		String[] winYear=request.getParameterValues("winYear");  //获奖年份
		String[] awards=request.getParameterValues("awards");    //获奖奖项
		
		InfoArticleAwards[] infoArticleAwards=new InfoArticleAwards[winYear.length];
		infoArticle.setPublisher(publisher);
		infoArticle.setPubDate(new Date());
		infoArticledao.save(infoArticle);    //保存文章信息
		
		for(int i=0;i<winYear.length;i++)
		{
			infoArticleAwards[i]=new InfoArticleAwards(infoArticle.getAId(), Integer.parseInt(winYear[i]), awards[i]);
			infoArticleAwardsdao.save(infoArticleAwards[i]);
		}
		
		//request.setAttribute("article", infoArticle);
		//request.setAttribute("articleawards", infoArticleAwards);
		return "redirect:show/article_"+infoArticle.getId();
	}
	
	//4.研究生获国家、江苏省科研创新计划资助情况
	@RequestMapping("submit_support")
	public String submitSupport(HttpServletRequest request,InfoSupport infoSupport)
	{
		HttpSession session=request.getSession();
		String publisher=(String) session.getAttribute("Name");
		infoSupport.setPublisher(publisher);
		infoSupport.setPubDate(new Date());
		infoSupportdao.save(infoSupport);    //保存信息

		return "redirect:show/support_"+infoSupport.getSId();
	}
	
	//5.团队建设
	@RequestMapping("submit_groupbuild")
	public String submitGroupBuild(HttpServletRequest request,InfoGroupBuild infoGroupBuild)
	{
		HttpSession session=request.getSession();
		String publisher=(String) session.getAttribute("Name");
		infoGroupBuild.setPublisher(publisher);
		infoGroupBuild.setPubDate(new Date());
		infoGroupBuilddao.save(infoGroupBuild);    //保存信息

		return "redirect:show/groupbuild_"+infoGroupBuild.getGId();
	}
	
	//6.出版教材
	@RequestMapping("submit_book")
	public String submitBook(HttpServletRequest request,InfoBook infoBook)
	{
		HttpSession session=request.getSession();
		String[] author=request.getParameterValues("author");  //获取作者列表
		
		String publisher=(String) session.getAttribute("Name");
		infoBook.setPublisher(publisher);
		infoBook.setPubDate(new Date());
		infoBookdao.save(infoBook);
		
		InfoBookWriter[] infoBookWriter=new InfoBookWriter[author.length];
		for(int i=0;i<author.length;i++)
		{
			infoBookWriter[i]=new InfoBookWriter(infoBook.getBookId(), author[i]);
			infoBookWriterdao.save(infoBookWriter[i]);
		}
		
		//request.setAttribute("book", infoBook);
		//request.setAttribute("bookwriter", infoBookWriter);
		return "redirect:show/book_"+infoBook.getBId();
	}

	//7.知识产权
	@RequestMapping("submit_ipr")
	public String submitIpr(HttpServletRequest request,InfoIpr infoIpr)
	{
		HttpSession session=request.getSession();
		String publisher=(String) session.getAttribute("Name");
		infoIpr.setPublisher(publisher);
		infoIpr.setPubDate(new Date());
		infoIprdao.save(infoIpr);    //保存信息
		
		return "redirect:show/ipr_"+infoIpr.getIprId();
	}
	
	//8.成果验收鉴定
	@RequestMapping("submit_checkup")
	public String submitCheckup(HttpServletRequest request,InfoCheckup infoCheckup)
	{
		HttpSession session=request.getSession();
		String publisher=(String) session.getAttribute("Name");
		infoCheckup.setPublisher(publisher);
		infoCheckup.setPubDate(new Date());
		infoCheckupdao.save(infoCheckup);    //保存信息

		return "redirect:show/checkup_"+infoCheckup.getCId();
	}
	
	//9.主办学术会议情况
	@RequestMapping("submit_meeting")
	public String submitMeeting(HttpServletRequest request,InfoMeeting infoMeeting)
	{
		HttpSession session=request.getSession();
		String publisher=(String) session.getAttribute("Name");
		infoMeeting.setPublisher(publisher);
		infoMeeting.setPubDate(new Date());
		infoMeetingdao.save(infoMeeting);    //保存信息

		return "redirect:show/meeting_"+infoMeeting.getMId();
	}
	
	//10.教师境外进修(合作研究)情况
	@RequestMapping("submit_foreign")
	public String submitForeign(HttpServletRequest request,InfoForeign infoForeign)
	{
		HttpSession session=request.getSession();
		String publisher=(String) session.getAttribute("Name");
		infoForeign.setPublisher(publisher);
		infoForeign.setPubDate(new Date());
		infoForeigndao.save(infoForeign);    //保存信息

		return "redirect:show/foreign_"+infoForeign.getFId();
	}
}
